package com.kayt.cyberBar.cpublic.business.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.property.PublicCodeConfigInfo;
import com.kayt.cyberBar.cuser.business.dao.IBPositionDao;
import com.kayt.cyberBar.cuser.business.entity.po.BPosition;

@Repository("publicPositionDao")
public class PublicPositionDaoImpl implements IBPositionDao {
	@Autowired
	private EntityDao entityDao;
	@Autowired
	private PublicCodeConfigInfo configInfo;

	@Override
	public Boolean checkExist(String name) throws SQLException {
		String hql = "select count(o.id) as count from BPosition o where o.positionName=?";
		List tempMapList;
		Boolean exist = false;
		tempMapList = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[] { name });
		if (tempMapList != null && !tempMapList.isEmpty()) {
			Map tempMap = (Map) tempMapList.get(0);
			Long tempCount = (Long) tempMap.get("count");
			if (tempCount > 0) {
				exist = true;
			}
		}
		return exist;
	}

	@Override
	public BPosition findById(String positionId) {
		if (StringUtils.isNotEmpty(positionId)) {
			Long id = null;
			id = Long.parseLong(positionId);
			return entityDao.findById(id, BPosition.class);
		} else {
			return null;
		}
	}

	@Override
	public Integer delete(String positionId) {
		if (StringUtils.isNotEmpty(positionId)) {
			Long id = null;
			id = Long.parseLong(positionId);
			BPosition BPosition = entityDao.findById(id, BPosition.class);
			if (BPosition != null) {
				entityDao.delete(BPosition);
				return 0;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public void modify(BPosition entity) throws SQLException {
		entityDao.update(entity);
	}

	@Override
	public PageData<BPosition> findByPage(String hql, Object[] params, LinkedHashMap<String, String> orderMap)
			throws SQLException {
		PageData<BPosition> positionPageData = this.entityDao.getPagingData(BPosition.class, -1, -1, hql, params,
				orderMap);
		return positionPageData;
	}

	@Override
	public BPosition save(BPosition entity) throws SQLException {
		Long id = entityDao.save(entity);
		entity.setId(id);
		return entity;
	}

	@Override
	public PageData<BPosition> getPositionList(String storeCode, LinkedHashMap<String, String> orderMap)
			throws SQLException {
		String hql = "o.storeCode = '" + storeCode + "' and o.statusCode != '" + configInfo.getPublicStatusDelete()
				+ "'";
		PageData<BPosition> positionPageData = this.entityDao.getPagingData(BPosition.class, -1, -1, hql, null,
				orderMap);
		return positionPageData;
	}

	@Override
	public List<BPosition> getPositionByStoreCode(String storeCode) throws SQLException {
		String sql = "select * from b_position where STORE_CODE = '" + storeCode + "'  order by id asc";
		return entityDao.getPagingDataBySql(BPosition.class, -1, -1, sql, null);
	}

	@Override
	public Map<String, BPosition> getPositionMapByStoreCode(String storeCode) throws SQLException {
		String sql = "select * from b_position where STORE_CODE = '" + storeCode + "'";
		List<BPosition> positionList = entityDao.getPagingDataBySql(BPosition.class, -1, -1, sql, null);
		Map<String, BPosition> positionMap = new HashMap<String, BPosition>();
		Iterator<BPosition> positionIter = positionList.iterator();
		while (positionIter.hasNext()) {
			BPosition bPosition = positionIter.next();
			positionMap.put(bPosition.getPositionCode(), bPosition);
		}
		return positionMap;
	}

	@Override
	public BPosition getByCode(String positionCode) throws SQLException {
		String sql = "select * from b_position where POSITION_CODE = '" + positionCode + "'";
		List<BPosition> positionList = entityDao.getPagingDataBySql(BPosition.class, -1, -1, sql, null);
		if (positionList != null && positionList.size() != 0) {
			return positionList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public Map<String, BPosition> findAll() throws SQLException {
		Map<String, BPosition> resultMap = new HashMap<>();
		List<BPosition> resultList = entityDao.getPagingDataBySql(BPosition.class, -1, -1,
				"select * from b_position where STATUS_CODE != '" + configInfo.getPublicStatusDelete() + "'  order by id asc", null);
		if (resultList == null || resultList.size() == 0) {
			return resultMap;
		} else {
			Iterator<BPosition> positionIter = resultList.iterator();
			while (positionIter.hasNext()) {
				BPosition position = positionIter.next();
				resultMap.put(position.getPositionCode(), position);
			}
			return resultMap;
		}

	}
}
